import { ref } from 'vue'
import apiService from '../services/api'

/**
 * API服务Hook
 * 提供对API服务的便捷访问
 */
export function useApi() {
  const loading = ref(false)
  const error = ref(null)

  // 用户认证API
  const login = async (username, password) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.login(username, password)
    } catch (err) {
      error.value = err
      console.error('登录失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const getUserInfo = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.getUserInfo()
    } catch (err) {
      error.value = err
      console.error('获取用户信息失败:', err)
      return null
    } finally {
      loading.value = false
    }
  }

  const logout = () => {
    apiService.logout()
  }

  // 分类管理API
  const getCategories = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.categories.getList()
    } catch (err) {
      error.value = err
      console.error('获取分类列表失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const createCategory = async (data) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.categories.create(data)
    } catch (err) {
      error.value = err
      console.error('创建分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const updateCategory = async (id, data) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.categories.update(id, data)
    } catch (err) {
      error.value = err
      console.error('更新分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const deleteCategory = async (id) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.categories.delete(id)
    } catch (err) {
      error.value = err
      console.error('删除分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 项目管理API
  const getItems = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.items.getList()
    } catch (err) {
      error.value = err
      console.error('获取项目列表失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const createItem = async (data) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.items.create(data)
    } catch (err) {
      error.value = err
      console.error('创建项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const updateItem = async (id, data) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.items.update(id, data)
    } catch (err) {
      error.value = err
      console.error('更新项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const deleteItem = async (id) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.items.delete(id)
    } catch (err) {
      error.value = err
      console.error('删除项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 页面管理API
  const getPages = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.pages.getList()
    } catch (err) {
      error.value = err
      console.error('获取页面列表失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const createPage = async (data) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.pages.create(data)
    } catch (err) {
      error.value = err
      console.error('创建页面失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const updatePage = async (id, data) => {
    loading.value = true
    error.value = null
    try {
      console.log('🔍 useApi - updatePage 调用:', { id, data })
      console.log('📊 useApi - 发送的数据:', JSON.stringify(data, null, 2))
      const result = await apiService.pages.update(id, data)
      console.log('✅ useApi - updatePage API 响应:', result)
      return result
    } catch (err) {
      error.value = err
      console.error('❌ useApi - 更新页面失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const deletePage = async (id) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.pages.delete(id)
    } catch (err) {
      error.value = err
      console.error('删除页面失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 层级数据API
  const getHierarchyData = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.getData()
    } catch (err) {
      error.value = err
      console.error('获取层级数据失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 页面项目关联关系管理
  const addItemToPageItem = async (pageItemId, itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.addItemToPageItem(pageItemId, itemId)
    } catch (err) {
      error.value = err
      console.error('添加关联项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const removeItemFromPageItem = async (pageItemId, itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.removeItemFromPageItem(pageItemId, itemId)
    } catch (err) {
      error.value = err
      console.error('移除关联项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const getPageItemRelations = async (itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.getPageItemRelations(itemId)
    } catch (err) {
      error.value = err
      console.error('获取项目关联关系失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 分类项目关系管理
  const addItemToCategory = async (categoryId, itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.addItemToCategory(categoryId, itemId)
    } catch (err) {
      error.value = err
      console.error('添加项目到分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  const removeItemFromCategory = async (categoryId, itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.removeItemFromCategory(categoryId, itemId)
    } catch (err) {
      error.value = err
      console.error('从分类移除项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取项目及其分类信息
   * @param {number} itemId - 项目ID
   * @returns {Promise} API响应，包含项目信息和关联的分类
   */
  const getItemWithCategories = async (itemId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.items.getWithCategory(itemId)
    } catch (err) {
      error.value = err
      console.error('获取项目分类信息失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取可用分类列表
   * @returns {Promise} API响应，包含可用分类列表
   */
  const getAvailableCategories = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.getAvailableCategories()
    } catch (err) {
      error.value = err
      console.error('获取可用分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取可用项目列表
   * @returns {Promise} API响应，包含可用项目列表
   */
  const getAvailableItems = async () => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.getAvailableItems()
    } catch (err) {
      error.value = err
      console.error('获取可用项目失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  /**
   * 添加分类到页面
   * @param {number} pageId - 页面ID
   * @param {number} categoryId - 分类ID
   * @returns {Promise} API响应
   */
  const addCategoryToPage = async (pageId, categoryId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.addCategoryToPage(pageId, categoryId)
    } catch (err) {
      error.value = err
      console.error('添加分类到页面失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  /**
   * 从页面移除分类
   * @param {number} pageId - 页面ID
   * @param {number} categoryId - 分类ID
   * @returns {Promise} API响应
   */
  const removeCategoryFromPage = async (pageId, categoryId) => {
    loading.value = true
    error.value = null
    try {
      return await apiService.hierarchy.removeCategoryFromPage(pageId, categoryId)
    } catch (err) {
      error.value = err
      console.error('从页面移除分类失败:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  return {
    // 状态
    loading,
    error,
    
    // 用户认证
    login,
    getUserInfo,
    logout,
    
    // 分类管理
    getCategories,
    createCategory,
    updateCategory,
    deleteCategory,
    
    // 项目管理
    getItems,
    createItem,
    updateItem,
    deleteItem,
    
    // 页面管理
    getPages,
    createPage,
    updatePage,
    deletePage,
    
    // 层级数据
    getHierarchyData,
    
    // 页面项目关联关系
    addItemToPageItem,
    removeItemFromPageItem,
    getPageItemRelations,
    
    // 分类项目关系管理
    addItemToCategory,
    removeItemFromCategory,
    
    // 项目分类信息获取
    getItemWithCategories,
    
    // 可用数据获取
    getAvailableCategories,
    getAvailableItems,
    
    // 分类页面关系管理
    addCategoryToPage,
    removeCategoryFromPage
  }
}